<?php

namespace app\controller;

use app\repository\IndexRepository;
use support\Request;
use Tinywan\Jwt\JwtToken;

class IndexController extends BaseController
{
    protected $repository;

    public function __construct(IndexRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index(Request $request)
    {
        $data = [
            "id"=>1,
            "name"=>"test",
            "email"=>"fuck",
            "password"=> password_hash("123456",PASSWORD_DEFAULT)
        ];
//        $password_hash =
        $user = JwtToken::generateToken($data);
        return $this->success([
            "data"=>$data,
            "user"=>$user
        ]);
    }

    public function test(Request $request)
    {
        $all = $request->all();
        return $this->success($all);
    }

    public function login(Request $request)
    {
        $username = $request->input("username");
        $password = $request->input("password");
        if (!$username || !$password){
            return $this->error("请输入用户名和密码");
        }
        $result = $this->repository->login($username, $password);
        return $this->res($result);
    }

    public function register(Request $request)
    {
        $data['name'] = $request->input("name");
        $data['username'] = $request->input("username");
        $data['password'] = $request->input("password");
        if (!$data['name'] || !$data['password'] || !$data['username']){
            return $this->error("您的信息输入不完整，请检查后提交");
        }
        $register = $this->repository->register($data);
        return $this->res($register);
    }

}
